import { diffArr } from "@/util/xc/utils";

class SystemFormula {
  constructor() {
    this.init();
  }

  /** 设置数据 */
  setFormula(formulaTypes = [], formulas = [], columnPy) {
    const { systemFormulaTypes, systemFormulas } = this.filter(columnPy, true);

    this.systemFormulaTypes = [...systemFormulaTypes, ...formulaTypes];
    this.systemFormulas = [...systemFormulas, ...formulas];
  }

  /** 获取指定数据 */
  getFormula(columnPy) {
    return this.filter(columnPy);
  }
  diffArr() {
    return diffArr(this.systemFormulaTypes, this._systemFormulaTypes) || diffArr(this.systemFormulas, this._systemFormulas);
  }

  /** 获取全部数据 */
  getParams() {
    const { systemFormulaTypes, systemFormulas } = this;
    return { systemFormulaTypes, systemFormulas };
  }

  /** 筛选 */
  filter(columnPy, isNot = false) {
    const systemFormulaTypes = this.systemFormulaTypes.filter(o => isNot ? o.columnPy != columnPy : o.columnPy == columnPy);
    const systemFormulas = this.systemFormulas.filter(o => isNot ? o.columnPy != columnPy : o.columnPy == columnPy);
    return { systemFormulaTypes, systemFormulas };
  }

  /** 初始化 */
  init(systemFormulaTypes = [], systemFormulas = []) {
    this.systemFormulaTypes = this.deepCopy(systemFormulaTypes);
    this._systemFormulaTypes = this.deepCopy(systemFormulaTypes);
    this.systemFormulas = this.deepCopy(systemFormulas);
    this._systemFormulas = this.deepCopy(systemFormulas);
  }

  /** 拷贝数据 */
  deepCopy(o) {
    return JSON.parse(JSON.stringify(o));
  }
}

let instance;
export default (() => {
  if (!instance) instance = new SystemFormula();
  window.aaa = instance;
  return instance;
})();
